package uit.minito.views;

import java.util.ArrayList;

import uit.minito.suggestion.RouterInfoAdapter;
import uit.minito.timduongxebuyt.R;
import uit.minito.timduongxebuyt.StaticVariables;
import android.app.Activity;
import android.content.Context;
import android.text.method.HideReturnsTransformationMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RouterInfoViewer extends RelativeLayout{

	LinearLayout mainLayout;
	boolean isOffScreen;
	TextView infoHeader;
	ListView infoListView;
	Activity context;
	RouterInfoAdapter adapter;
	private void initialize(Context context){
		LayoutInflater layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.router_info, this);
		mainLayout = (LinearLayout)findViewById(R.id.routerInfoMainLayout);  
		RelativeLayout.LayoutParams mainParam=(RelativeLayout.LayoutParams)mainLayout.getLayoutParams();
  
		mainParam.topMargin=MyUtilities.convertToDP(StaticVariables.ROUTER_INFO_VIEWER_HEIGHT, getContext());
		mainParam.bottomMargin=mainParam.topMargin+MyUtilities.convertToDP(StaticVariables.ROUTER_INFO_VIEWER_HEIGHT, getContext());
		mainLayout.setLayoutParams(mainParam);
		//doSlideAnimation(0, StaticVariables.ROUTER_INFO_VIEWER_HEIGHT, 0);
		isOffScreen = true;  
		
		this.context = (Activity)context;
		infoHeader = (TextView)findViewById(R.id.routerInfoTextView);

		infoHeader.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				bringToFront();
				autoShowOrHide();
			}
		});
		
		infoListView = (ListView)findViewById(R.id.routerInfoListView);
	}    
	
	public void updateInfo(ArrayList<String> infoList){
		adapter=new RouterInfoAdapter(this.context, infoList);
		infoListView.setAdapter(adapter);
	}
	
	public RouterInfoViewer(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initialize(context);
	}

	public RouterInfoViewer(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initialize(context);
	}
	
	private void doSlideAnimation(float _fromY,final float _toY, long duration){
		TranslateAnimation translateAnimation=new TranslateAnimation(0, 0, MyUtilities.convertToDP(_fromY, getContext()), MyUtilities.convertToDP(_toY, getContext()));
		translateAnimation.setDuration(duration);
		translateAnimation.setFillBefore(true);
		
		translateAnimation.setAnimationListener(new Animation.AnimationListener() {
			
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				

				RelativeLayout.LayoutParams mainParam=(RelativeLayout.LayoutParams)mainLayout.getLayoutParams();
				if(isOffScreen){
					mainParam.topMargin=0;
				}else{
					mainParam.topMargin=MyUtilities.convertToDP(_toY, getContext());
				}
				mainParam.bottomMargin=mainParam.topMargin+MyUtilities.convertToDP(_toY, getContext());
				mainLayout.setLayoutParams(mainParam);
				isOffScreen=!isOffScreen;
				mainLayout.clearAnimation();

			}
		});
		
		mainLayout.startAnimation(translateAnimation);
	}
	
	public void autoShowOrHide(){
		if(isOffScreen){
			doSlideAnimation(0, -StaticVariables.ROUTER_INFO_VIEWER_HEIGHT, StaticVariables.SLIDE_DURATION);
		}else{
			doSlideAnimation(0, StaticVariables.ROUTER_INFO_VIEWER_HEIGHT, StaticVariables.SLIDE_DURATION);
		}
	}

}
